//
//  EPhoto.m
//  Common
//
//  Created by guoshiqing on 2018/6/1.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "EPhoto.h"
#import <QuartzCore/QuartzCore.h>
#import "UIImageView+WebCache.h"

@interface EPhoto () <UIGestureRecognizerDelegate>
@property (nonatomic) CGPoint pointOrigin;
@property (nonatomic) BOOL editModel;
@property (nonatomic) PhotoType type;
@end

@implementation EPhoto

- (id)initWithOrigin:(CGPoint)origin withSize:(CGFloat)size
{
    self = [super initWithFrame:CGRectMake(origin.x, origin.y, size, size)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        self.viewPhoto = [[UIImageView alloc] initWithFrame:self.bounds];
        self.viewPhoto.clipsToBounds = YES;
        self.viewPhoto.contentMode = UIViewContentModeScaleAspectFill;
        self.viewPhoto.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//        self.viewPhoto.layer.cornerRadius = 12;
        //        self.viewPhoto.layer.masksToBounds = YES;
        
        // 添加删除按钮
        self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.delBtn.frame = CGRectMake(self.bounds.size.width - 25, 5, 20, 20);
        [self.delBtn setTitle:@" × " forState:UIControlStateNormal];
        self.delBtn.alpha = 0.6;
        [self.delBtn setBackgroundColor:[UIColor blackColor]];
        self.delBtn.layer.cornerRadius = 10;
        self.delBtn.layer.masksToBounds = YES;
        [self.delBtn addTarget:self action:@selector(btnDelClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self addSubview:self.viewPhoto];
        [self addSubview:self.delBtn];
        
        // 添加单指单击手势,放大查看图片
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick2Enlarge:)];
        tapRecognizer.numberOfTouchesRequired = 1;
        tapRecognizer.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tapRecognizer];
        
        self.editModel = NO;
    }
    return self;
}

- (void)setPhotoType:(PhotoType)type
{
    self.type = type;
    if (type == PhotoTypeAdd) {
        self.viewPhoto.image = [UIImage imageNamed:@"andphoto80"];
        [self.delBtn setHidden:YES];
        
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick2Add:)];
        tapRecognizer.numberOfTouchesRequired = 1;
        tapRecognizer.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tapRecognizer];
    }
}

- (PhotoType)getPhotoType
{
    return self.type;
}

- (void)setPhotoUrl:(id)photoUrl resouceType:(RESOURCETYPE)type
{
    if (RESOURCE_NET == type) {
        NSString *url = (NSString *)photoUrl;
        [self.viewPhoto sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:(SDWebImageRetryFailed | SDWebImageAllowInvalidSSLCertificates)];
        [self.delBtn setHidden:YES];
    }
    else if (RESOURCE_LOCAL == type)
    {
        UIImage *image = (UIImage *)photoUrl;
        [self.viewPhoto setImage:image];
//#warning 隐藏删除按钮
//        [self.delBtn setHidden:YES];
    }
}


- (void)moveToPosition:(CGPoint)point
{
    if (self.type == PhotoTypePhoto) {
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
        } completion:nil];
    } else {
        self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
    }
}

- (void)setEditModel:(BOOL)edit
{
    if (self.type == PhotoTypePhoto) {
        if (edit) {
            UILongPressGestureRecognizer *longPressreRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
            longPressreRecognizer.delegate = self;
            [self addGestureRecognizer:longPressreRecognizer];
        } else {
            for (UIGestureRecognizer *recognizer in [self gestureRecognizers]) {
                if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
                    [self removeGestureRecognizer:recognizer];
                    break;
                }
            }
        }
    }
}

#pragma mark - UIGestureRecognizer
#pragma mark - 编辑模式下，点击添加按钮
- (void)tapClick2Add:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(photoAdd:)]) {
        [self.delegate photoAdd:self];
    }
}

#pragma mark - 编辑或者展示模式下，点击放大常看
- (void)tapClick2Enlarge:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(photoEnlarge:)]) {
        [self.delegate photoEnlarge:self];
    }
}


#pragma mark - 删除按钮&操作
- (void)btnDelClick:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(photoDel:)]) {
        [self.delegate photoDel:self];
    }
}

- (void)handleLongPress:(id)sender
{
    UILongPressGestureRecognizer *recognizer = sender;
    CGPoint point = [recognizer locationInView:self];
    
    CGFloat diffx = 0.;
    CGFloat diffy = 0.;
    
    if (UIGestureRecognizerStateBegan == recognizer.state) {
        self.pointOrigin = point;
        [self.superview bringSubviewToFront:self];
    } else if (UIGestureRecognizerStateEnded == recognizer.state) {
        if ([self.delegate respondsToSelector:@selector(photoMoveFinished:)]) {
            [self.delegate photoMoveFinished:self];
        }
    } else {
        diffx = point.x - self.pointOrigin.x;
        diffy = point.y - self.pointOrigin.y;
    }
    
    CGFloat originx = self.frame.origin.x +diffx;
    CGFloat originy = self.frame.origin.y +diffy;
    
    self.frame = CGRectMake(originx, originy, self.frame.size.width, self.frame.size.height);
}

@end
